/**
 * 
 */
package com.jacen.fzxs.service.impl;

import java.util.List;

import jodd.util.StringUtil;

import org.springframework.transaction.annotation.Transactional;

import com.jacen.fzxs.dao.IBaseDao;
import com.jacen.fzxs.service.IBaseService;


/**
 * 公共的service基类
 * 
 * @author yanzihui 2013-4-23
 */
@Transactional(rollbackFor = Exception.class)
public abstract class BaseService<T> implements
		IBaseService<T>{

	/**
	 * @return
	 * yanzihui 2013-11-22 下午4:31:48
	 */
	@Override
	public abstract IBaseDao<T> getDao();

	/**新增
	 * @param e
	 * @throws Exception
	 * yanzihui 2013-11-22 下午4:31:48
	 */
	@Override
	public void insert(T e) throws Exception {
		getDao().insert(e);
	}
	
	@Override
	public void insert(List<T> entities) throws Exception {
		getDao().insert(entities);
	}

	/**修改
	 * @param e
	 * @throws Exception
	 * yanzihui 2013-11-22 下午4:31:48
	 */
	@Override
	public void update(T e) throws Exception {
		getDao().update(e);
	}
	
	public void update(T entity, String keyColumn) throws Exception{
		getDao().update(entity, keyColumn);
	}
	
	public int[] update(List<T> entities, String keyColumn) throws Exception{
		return getDao().update(entities, keyColumn);
	}

	/**删除
	 * @param e
	 * yanzihui 2013-11-22 下午4:31:48
	 */
	@Override
	public void delete(T e) {
		getDao().delete(e);
	}

	/**根据关键ID删除
	 * @param id
	 * yanzihui 2013-11-22 下午4:31:48
	 */
	@Override
	public void deleteById(String id) {
		getDao().deleteById(id);
	}
	
	/**根据关键字段删除
	 * @param keyColumn
	 * @param value
	 * @throws Exception
	 * yanzihui 2013-11-22 下午4:31:48
	 */
	@Override
	public void deleteByKey(String keyColumn, Object value) throws Exception{
		if (StringUtil.isEmpty(keyColumn))
			throw new Exception("关键字段名不能为空");
		if (value == null)
			throw new Exception("字段值不能为空");
		getDao().deleteByKey(keyColumn, value);
	}

	/**根据关键ID获取
	 * @param id
	 * @return
	 * yanzihui 2013-11-22 下午4:31:48
	 */
	@Override
	@Transactional(readOnly = true)
	public T getById(String id) {
		return getDao().getById(id);
	}

	/**根据关键字段获取实体
	 * @param keyColumn
	 * @param value
	 * @return T
	 * @throws Exception
	 * yanzihui 2013-11-22 下午4:31:48
	 */
	@Override
	@Transactional(readOnly = true)
	public T getByKey(String keyColumn, Object value)
			throws Exception {
		if (StringUtil.isEmpty(keyColumn))
			throw new Exception("关键字段名不能为空");
		if (value == null)
			throw new Exception("字段值不能为空");
		return getDao().getByKey(keyColumn, value);
	}
	
	/**根据关键字段判断是否存在
	 * @param keyColumn
	 * @param value
	 * @return boolean
	 * @throws Exception
	 * yanzihui 2013-11-22 下午4:31:48
	 */
	@Override
	@Transactional(readOnly = true)
	public boolean existByKey(String keyColumn, Object value) throws Exception{
		if (StringUtil.isEmpty(keyColumn))
			throw new Exception("关键字段名不能为空");
		if (value == null)
			throw new Exception("字段值不能为空");
		return getDao().existByKey(keyColumn, value);
	}

	/**获取所有
	 * @return
	 * yanzihui 2013-11-22 下午4:31:48
	 */
	@Override
	@Transactional(readOnly = true)
	public List<T> getAll() {
		return getDao().getAll();
	}

	/**获取所有记录数
	 * @return
	 * yanzihui 2013-11-22 下午4:31:48
	 */
	@Override
	@Transactional(readOnly = true)
	public Long getAllCount() {
		return getDao().getAllCount();
	}

	/**根据ID字段判断是否存在 
	 * @param id
	 * @return
	 * yanzihui 2013-11-22 下午4:31:48
	 */
	@Override
	@Transactional(readOnly = true)
	public boolean existById(String id) {
		return getDao().existById(id);
	}

	/**
	 * 删除全部
	 * yanzihui 2013-11-22 下午4:31:48
	 */
	@Override
	public void deleteAll() {
		 getDao().deleteAll();
	}
	
	public int clearOldData(int monthCount, String dateColName){
		return getDao().clearOldData(monthCount, dateColName);
	}
	
}
